'use strict';
const dayjs = require("dayjs");
module.exports = app => {
  const { STRING, INTEGER, TEXT, DATE, DECIMAL } = app.Sequelize;
  const Group = app.model.define(
    'Group',
    {
      id: {type: INTEGER, primaryKey: true, autoIncrement: true},
      name: STRING, //对象名称
      desc: STRING, // 对象描述
      url: STRING, // 图片地址
      password: STRING, // 密码
      status: INTEGER,
      create_time: {
        type: DATE,
        get() {
          return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
        }
      },
    },
    {
      tableName: 'group',
      timestamps: true, // 启用时间戳(createdAt, updatedAt)
      updatedAt: 'updated_time', // 不想要 updatedAt
      createdAt: 'create_time', // 想要  createdAt 但是希望名称叫做 created_at
    }
  );
  Group.associate = function() {
    app.model.Group.hasMany(app.model.Vote, {foreignKey: 'group_id', sourceKey: 'id', as: 'voteList'});
  }
  return Group
};
